package org.sy.lab.多线程基础._4_执行器._3_分离任务启动和结束的处理;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class Consumer implements Runnable {

	private String name;
	private boolean end = false;
	private CompletionService<String> service;
	
	public Consumer(String name, CompletionService<String> service) {
		this.name = name;
		this.service = service;
	}
	
	@Override
	public void run() {
		while(!end) {
			Future<String> result = service.poll();
			try {
				if(result != null) {
					System.out.println(name + "消费 " + result.get());
				}
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void setEnd() {
		end = true;
	}
}
